package com.ld.admin.controller.usercenter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.validation.Valid;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.usercenter.SysUserNoticeMediaModel;
import com.ld.common.util.notice.NoticeManager;
import com.ld.common.util.notice.NoticeModel;
import com.ld.common.util.notice.NoticeUtil;
import com.ld.shieldsb.annotation.duplicate.SubmitRequestIntercept;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 用户通知媒介
 * 
 * @ClassName SysUserNoticeMediaController
 * @author <a href="mailto:donggongai1@126.com" target="_blank">吕凯</a>
 * @date 2019年01月29日 09:50:15
 *
 */
@Api(tags = "用户通知媒介")
@ConditionalOnProperty(prefix = "modules.sc.usercenter", name = "noticemedia.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "usercenterUserNoticeMediaController")
@RequestMapping("${shieldsb.adminPath}/usercenter/noticemedia")
public class UserNoticeMediaController extends AdminBasicController<SysUserNoticeMediaModel> {

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/usercenter/noticemedia/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/usercenter/noticemedia/sysusernoticemedia_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        QueryModel queryModel = basicService.getSearchQueryModel(request);
        queryModel.addEq("userId", Web.Util.getSessionUserId(request));
        basicService.getList(request, SysUserNoticeMediaModel.class, queryModel);
        return "a/usercenter/noticemedia/sysusernoticemedia_list";
    }

    @ApiOperation(value = "用户通知媒介展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        basicService.getOne(request, getModelClass(), id);
        return "a/usercenter/noticemedia/sysusernoticemedia_show";
    }

    @ApiOperation(value = "跳转到添加/修改用户通知媒介", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        long id = getLong("id");
        QueryModel queryModel = basicService.getValidQueryModel();
        queryModel.addEq("id", id);
        queryModel.addEq("userId", Web.Util.getSessionUserId(request));
        basicService.getOne(request, getModelClass(), queryModel);
        Map<String, NoticeModel> noticesMap = NoticeManager.getAllNoticeModels();
        request.setAttribute("noticeTypes", noticesMap);
        return "a/usercenter/noticemedia/sysusernoticemedia_update";
    }

    @ApiOperation(value = "添加/修改用户通知媒介", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object addOrUpd(@Valid SysUserNoticeMediaModel model) {
        model.setUserId(Web.Util.getSessionUserId(request) + "");
        NoticeModel mocieModel = NoticeManager.get(model.getMediaType());
        if (mocieModel != null) {
            model.setMediaTypeName(mocieModel.getName());
        }
        Result result = basicService.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "停用用户通知媒介", notes = "")
    @RequestMapping(path = "/disable", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object disable() throws ServletException, IOException {
        long id = getLong("id");
        Result result = basicService.disable(getModelClass(), id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "启用用户通知媒介", notes = "")
    @RequestMapping(path = "/enable", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object enable() throws ServletException, IOException {
        long id = getLong("id");
        Result result = basicService.enable(getModelClass(), id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除用户通知媒介", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = basicService.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "跳转到测试发送通知页面", notes = "")
    @RequestMapping(path = "/toSendTest", method = RequestMethod.GET)
    public String toSend() {
        setAttributes();
        return "a/usercenter/noticemedia/sysusernoticemedia_sendtest";
    }

    @ApiOperation(value = "测试发送通知", notes = "")
    @RequestMapping(path = "/sendtest", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object send() {
        long id = getLong("id");
        QueryModel queryModel = basicService.getValidQueryModel();
        queryModel.addEq("id", id);
        queryModel.addEq("userId", Web.Util.getSessionUserId(request));
        SysUserNoticeMediaModel noticeMediaModel = basicService.getOne(request, getModelClass(), queryModel);
        if (noticeMediaModel != null) {
            if (noticeMediaModel.getState() == null || noticeMediaModel.getState() != 0) {
                return Web.Response.error("通知媒介未启用！");
            }
            String content = getString("content");
//            NoticeActionModel noticeModel = new NoticeActionModel();
//            noticeModel.setTitle("个人通知");
//            noticeModel.setType(noticeMediaModel.getMediaType());
//            noticeModel.setSendToUser(Web.Util.getSessionUserId(request) + "");
//            Result result = NoticeUtil.sendNotice(noticeModel, content);
            Result result = NoticeUtil.sendNotice("个人通知", content, noticeMediaModel.getMediaType(), noticeMediaModel.getSendTo());
            if (result.getSuccess()) {
                if (StringUtils.isNotEmpty(result.getMessage())) {
                    return Web.Response.success(result.getMessage());
                }
                return Web.Response.success("发送成功！");
            } else {
                return Web.Response.error("发送失败！" + result.getMessage());
            }
        }
        return Web.Response.error("通知媒介不存在！");
    }

}